﻿package Game.MouseSystem{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	
	import Game.Manager;
	import Game.Utils.T;
	public class MouseManager extends Manager{
		
		private var m_GraphicWorld:MovieClip;
		private var ScrollSpeed : int = 3;
		private static var m_clickableObjs:Array;
		
		public function MouseManager(_parentObj:*,_world:MovieClip):void{
			super(_parentObj);
			m_GraphicWorld = _world;
			m_clickableObjs = new Array();
		}
		
		public override function addAllListeners():void{
			m_GraphicWorld.addEventListener(Event.ENTER_FRAME, onMouseFrame);
			m_GraphicWorld.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			m_GraphicWorld.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			m_GraphicWorld.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownAction);
			
		}
		private function onMouseFrame(_evt:Event):void{
			//T.Trace (Parent.mouseX+"   "+Parent.mouseY);
			if(Parent.mouseY<=500){
				if(Parent.mouseX >= 700){
					m_GraphicWorld.x-=ScrollSpeed;
				}
				if(Parent.mouseX <= 100){
					m_GraphicWorld.x+=ScrollSpeed;
				}
				if(Parent.mouseY <= 100){
					m_GraphicWorld.y+=ScrollSpeed;
				}
				if(Parent.mouseY >= 400  ){
					m_GraphicWorld.y-=ScrollSpeed;
				}
			}
		}
		private function onMouseOver(_evt:MouseEvent):void{
			//--> nom de case trace(_evt.target.name);
			_evt.target.alpha=0.5;
		}
		private function onMouseOut(_evt:MouseEvent):void{
			_evt.target.alpha=1;
		}
		private function onMouseDownAction(_evt:MouseEvent):void{
			var found:Boolean=false;
			for(var i:int=0; i< m_clickableObjs.length;i++){
				if(_evt.target == m_clickableObjs[i][0]){
					m_clickableObjs[i][1]();
					found=true;
				}
			}
			if(!found){
				T.Trace("No object Found",6,"MouseManager/Click");
			}
			
		}
		public static function registerClickable(_obj:MovieClip,_action:Function,_over_action:Function = null):void{
			//_obj.addEventListener(MouseEvent.MOUSE_DOWN,_action);
			if(_over_action != null){
				//_obj.addEventListener(MouseEvent.MOUSE_OVER,_over_action);
			}
			var objectInfo:Array = new Array(_obj,_action,_over_action);
			m_clickableObjs.push(objectInfo);
		}
		public static function unregisterClickable(_obj:MovieClip):void{
			for(var i:int=0; i< m_clickableObjs.length;i++){
				if(m_clickableObjs[i][0] == _obj){
					//m_clickableObjs[i][0].removeEventListener(MouseEvent.MOUSE_DOWN,m_clickableObjs[i][1]);
					if(m_clickableObjs[i][2]!=null){
						//m_clickableObjs[i][0].removeEventListener(MouseEvent.MOUSE_OVER,m_clickableObjs[i][2]);
					}
				}
				else{
					T.Trace("Unrecognised Object",99,"MouseManaeger/Unregister");
				}
			}
		}
		
		
		
		
	}
}